/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.transaction.support;

import org.springframework.lang.Nullable;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;

/**
 * Extension of the {@link org.springframework.transaction.PlatformTransactionManager}
 * interface, exposing a method for executing a given callback within a transaction.
 *
 * <p>Implementors of this interface automatically express a preference for
 * callbacks over programmatic {@code getTransaction}, {@code commit}
 * and {@code rollback} calls. Calling code may check whether a given
 * transaction manager implements this interface to choose to prepare a
 * callback instead of explicit transaction demarcation control.
 *
 * <p>Spring's {@link TransactionTemplate} and
 * {@link org.springframework.transaction.interceptor.TransactionInterceptor}
 * detect and use this PlatformTransactionManager variant automatically.
 *
 * @author Juergen Hoeller
 * @since 2.0
 * @see TransactionTemplate
 * @see org.springframework.transaction.interceptor.TransactionInterceptor
 */
/*
1. 总结
CallbackPreferringPlatformTransactionManager 是 PlatformTransactionManager 的扩展接口
	它提供了基于回调机制的事务执行方式，优先使用回调而不是传统的编程式事务控制
	主要用于简化事务管理代码，提高代码可读性和减少样板代码
2. 内部成员说明
	execute(TransactionDefinition definition, TransactionCallback<T> callback) 方法:
		在事务中执行指定的回调操作
		允许返回在事务内创建的结果对象
		如果回调抛出 RuntimeException，则强制回滚事务
	该异常会传播给模板的调用者
3. 类关系结构
PlatformTransactionManager (interface)
└── CallbackPreferringPlatformTransactionManager (interface)
    └── MockCallbackPreferringTransactionManager (class)
与其他相关接口的区别：
	PlatformTransactionManager: 提供传统的 getTransaction、commit、rollback 编程式事务控制方法
	CallbackPreferringPlatformTransactionManager: 优先使用回调机制，通过 execute 方法封装事务边界，更简洁易用
4. 使用场景
	需要简化事务管理代码，避免手动控制事务边界
	希望采用函数式编程风格处理事务操作
	与 TransactionTemplate 或 TransactionInterceptor 配合使用
	需要在事务中执行并返回业务逻辑结果的场景
5. 实际代码应用示例
// 使用 TransactionTemplate（内部会检测 CallbackPreferringPlatformTransactionManager）
@Autowired
private CallbackPreferringPlatformTransactionManager transactionManager;

public void performTransactionalOperation() {
    TransactionTemplate template = new TransactionTemplate(transactionManager);

    template.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // 执行业务逻辑
            // 无需手动 commit 或 rollback
        }
    });
}

// 直接使用 execute 方法
public String performBusinessLogic() {
    return transactionManager.execute(
        new DefaultTransactionDefinition(),
        status -> {
            // 执行业务逻辑
            return "result";
        }
    );
}

 */
public interface CallbackPreferringPlatformTransactionManager extends PlatformTransactionManager {

	/**
	 * Execute the action specified by the given callback object within a transaction.
	 * <p>Allows for returning a result object created within the transaction, that is,
	 * a domain object or a collection of domain objects. A RuntimeException thrown
	 * by the callback is treated as a fatal exception that enforces a rollback.
	 * Such an exception gets propagated to the caller of the template.
	 * @param definition the definition for the transaction to wrap the callback in
	 * @param callback the callback object that specifies the transactional action
	 * @return a result object returned by the callback, or {@code null} if none
	 * @throws TransactionException in case of initialization, rollback, or system errors
	 * @throws RuntimeException if thrown by the TransactionCallback
	 */
	@Nullable
	<T> T execute(@Nullable TransactionDefinition definition, TransactionCallback<T> callback)
			throws TransactionException;

}
